#!/bin/bash

# HTTP服务器多平台编译控制脚本
# 支持gcc、riscv64和arm平台的自动化编译

set -e  # 遇到错误立即退出

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 打印带颜色的消息
print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 显示使用帮助
show_help() {
    echo "HTTP服务器多平台编译脚本"
    echo ""
    echo "用法: $0 [选项] [平台]"
    echo ""
    echo "平台选项:"
    echo "  gcc         编译本地GCC版本 (默认)"
    echo "  riscv64     编译RISC-V 64位版本"
    echo "  arm         编译ARM版本"
    echo "  all         编译所有平台版本"
    echo ""
    echo "其他选项:"
    echo "  -d, --debug     编译调试版本"
    echo "  -c, --clean     清理编译产物"
    echo "  -r, --run       编译后运行程序"
    echo "  -i, --install   编译后安装到系统"
    echo "  -h, --help      显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0                      # 默认编译gcc版本"
    echo "  $0 gcc                  # 编译gcc版本"
    echo "  $0 riscv64             # 编译RISC-V版本"
    echo "  $0 arm                 # 编译ARM版本"
    echo "  $0 all                 # 编译所有版本"
    echo "  $0 -d gcc              # 编译gcc调试版本"
    echo "  $0 -c                  # 清理编译产物"
    echo "  $0 -r gcc              # 编译并运行"
    echo ""
}

# 检查工具链是否可用
check_toolchain() {
    local toolchain=$1
    local name=$2
    
    if command -v ${toolchain}gcc >/dev/null 2>&1; then
        print_success "${name}工具链可用: ${toolchain}gcc"
        return 0
    else
        print_warning "${name}工具链不可用: ${toolchain}gcc"
        return 1
    fi
}

# 检查编译环境
check_environment() {
    print_info "检查编译环境..."
    
    # 检查基本工具
    if ! command -v make >/dev/null 2>&1; then
        print_error "make工具未找到，请安装build-essential"
        exit 1
    fi
    
    # 检查各平台工具链（不强制退出，使用|| true忽略返回值）
    check_toolchain "" "GCC" || true
    check_toolchain "riscv64-anyka-linux-musl-" "RISC-V 64位" || true
    check_toolchain "arm-nextvpu-linux-gnueabihf-" "ARM" || true
    
    print_info "环境检查完成"
    echo ""
}

# 编译指定平台
build_platform() {
    local platform=$1
    local debug_flag=$2
    
    print_info "开始编译${platform}版本..."
    
    # 根据平台选择编译目标
    case $platform in
        gcc)
            if [ "$debug_flag" = "true" ]; then
                make debug
            else
                make gcc
            fi
            ;;
        riscv64)
            if [ "$debug_flag" = "true" ]; then
                make CROSS_COMPILE=riscv64-anyka-linux-musl- TARGET=http_server_riscv64 CFLAGS="-Wall -g -DDEBUG -O0 -std=c99" clean http_server_riscv64
            else
                make riscv64
            fi
            ;;
        arm)
            if [ "$debug_flag" = "true" ]; then
                make CROSS_COMPILE=arm-nextvpu-linux-gnueabihf- TARGET=http_server_arm CFLAGS="-Wall -g -DDEBUG -O0 -std=c99" clean http_server_arm
            else
                make arm
            fi
            ;;
        all)
            if [ "$debug_flag" = "true" ]; then
                print_info "编译所有平台的调试版本..."
                build_platform gcc true
                build_platform riscv64 true
                build_platform arm true
            else
                make all-platforms
            fi
            ;;
        *)
            print_error "不支持的平台: $platform"
            print_info "支持的平台: gcc, riscv64, arm, all"
            exit 1
            ;;
    esac
    
    if [ $? -eq 0 ]; then
        print_success "${platform}版本编译完成"
    else
        print_error "${platform}版本编译失败"
        exit 1
    fi
}

# 主函数
main() {
    local platform="gcc"  # 默认平台
    local debug_flag="false"
    local clean_flag="false"
    local run_flag="false"
    local install_flag="false"
    
    # 解析命令行参数
    while [[ $# -gt 0 ]]; do
        case $1 in
            -d|--debug)
                debug_flag="true"
                shift
                ;;
            -c|--clean)
                clean_flag="true"
                shift
                ;;
            -r|--run)
                run_flag="true"
                shift
                ;;
            -i|--install)
                install_flag="true"
                shift
                ;;
            -h|--help)
                show_help
                exit 0
                ;;
            gcc|riscv64|arm|all)
                platform=$1
                shift
                ;;
            *)
                print_error "未知参数: $1"
                show_help
                exit 1
                ;;
        esac
    done
    
    # 显示脚本标题
    echo "========================================"
    echo "HTTP服务器多平台编译脚本"
    echo "========================================"
    echo ""
    
    # 清理编译产物
    if [ "$clean_flag" = "true" ]; then
        print_info "清理编译产物..."
        make clean
        print_success "清理完成"
        echo ""
    fi
    
    # 检查编译环境
    check_environment
    
    print_info "开始执行编译流程..."
    
    # 执行编译
    build_platform $platform $debug_flag
    echo ""
    
    # 安装到系统
    if [ "$install_flag" = "true" ]; then
        print_info "安装到系统..."
        make install
        print_success "安装完成"
        echo ""
    fi
    
    # 运行程序
    if [ "$run_flag" = "true" ]; then
        if [ "$platform" = "gcc" ] || [ "$platform" = "all" ]; then
            print_info "运行HTTP服务器..."
            ./http_server
        else
            print_warning "非gcc平台无法直接运行，请在对应设备上运行"
        fi
    fi
    
    # 显示编译结果
    print_success "编译流程完成！"
    echo ""
    print_info "生成的文件:"
    ls -la http_server* 2>/dev/null || print_warning "未找到编译产物"
}

# 脚本入口
main "$@"